﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_product_modifyProduct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int pid;
        if (!Page.IsPostBack)
        {
            try
            {
                pid = Convert.ToInt32(Request["pid"]);
            }
            catch (Exception)
            {
                return;
            }
            //读取产品信息，并填入相应的区域
            DB db = new DB();
            Product product = db.getProduct(pid);
            if (product != null)
            {
                nameTextBox_cn.Text = product.Name_cn;
                nameTextBox_en.Text = product.Name_en;
                descTextBox_cn.Text = product.Desc_cn;
                descTextBox_en.Text = product.Desc_en;
                newProductCheckBox.Checked = product.DimNew;
                recommendedProductCheckBox.Checked = product.Recommended;
                categoryList.SelectedValue = product.Category.ToString();
                productImage.ImageUrl = "~/pimages/" + product.Image;
                //用来记录额外信息
                idLabel.Text = product.ProductId.ToString();
                fileLabel.Text = product.Image;
            }
        }
    }

    //保存当前信息
    protected void saveButton_Click(object sender, EventArgs e)
    {
        string newFileName = null;
        //如果用户新替换了文件，那么就把原来的删除了
        if (imageFileUpload.HasFile)
        {
            newFileName = Utils.replaceFile(imageFileUpload, fileLabel.Text);
        }
        //如果用户没有上传新的文件的话，就不改变文件
        if (newFileName == null)
        {
            newFileName = fileLabel.Text;
        }
        int pid = Convert.ToInt32(idLabel.Text);
        string name_cn = nameTextBox_cn.Text;
        string name_en = nameTextBox_en.Text;
        string desc_cn = descTextBox_cn.Text;
        string desc_en = descTextBox_en.Text;
        DB db = new DB();
        db.modifyProduct(pid,name_cn,name_en,desc_cn,desc_en,recommendedProductCheckBox.Checked?1:0,newProductCheckBox.Checked?1:0,Convert.ToInt32(categoryList.SelectedValue),newFileName);

    }
}
